﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.MobileServices;
using System.Windows.Media.Imaging;

namespace mobileS.GoodDetails
{
    # region Clases
    public class GoodSet
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }

        [JsonProperty(PropertyName = "Name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "Latitude")]
        public decimal Latitude { get; set; }

        [JsonProperty(PropertyName = "Longitude")]
        public decimal Longitude { get; set; }

        [JsonProperty(PropertyName = "PriceDay")]
        public decimal PriceDay { get; set; }

        [JsonProperty(PropertyName = "SiteId")]
        public decimal SiteId { get; set; }

        [JsonProperty(PropertyName = "Likes")]
        public decimal Likes { get; set; }

        [JsonProperty(PropertyName = "UserId")]
        public decimal UserId { get; set; }
    }

    public class SiteAttributeSet
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }

        [JsonProperty(PropertyName = "Name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "SiteId")]
        public int SiteId { get; set; }
    }

    public class AttributeSet
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }

        [JsonProperty(PropertyName = "Value")]
        public string Value { get; set; }

        [JsonProperty(PropertyName = "GoodId")]
        public int GoodId { get; set; }

        [JsonProperty(PropertyName = "SiteAttributeId")]
        public int SiteAttributeId { get; set; }
    }

    public class AttrNameValue
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    # endregion

    public partial class GoodDetails : PhoneApplicationPage
    {
        public GoodDetails()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                int goodId = int.Parse(NavigationContext.QueryString["goodId"]);
                LoadGoods(goodId);
            }
            catch
            {
                NavigationService.GoBack();
            }
        }

        private async void LoadGoods(int goodId)
        {
            ListItems.ItemsSource = null;
            loadingText.Visibility = Visibility.Visible;
            loagindBar.Visibility = Visibility.Visible;
            IMobileServiceTable<GoodSet> goodsSetTable = App.MobileService.GetTable<GoodSet>();

            // Se obtienen el bien deseado.
            MobileServiceCollection<GoodSet, GoodSet> goods = await goodsSetTable.Where(goodItem => (goodItem.Id == goodId)).ToCollectionAsync();
            GoodSet good = goods.First();

            // Se obtienen los atributos del sitio.
            IMobileServiceTable<SiteAttributeSet> siteAttributeSetTable = App.MobileService.GetTable<SiteAttributeSet>();
            MobileServiceCollection<SiteAttributeSet, SiteAttributeSet> siteAttributes = await siteAttributeSetTable.Where(siteAttributeItem => (siteAttributeItem.SiteId == good.SiteId)).ToCollectionAsync();

            // Se obtienen los atributos del sitio.
            IMobileServiceTable<AttributeSet> attributeSetTable = App.MobileService.GetTable<AttributeSet>();
            MobileServiceCollection<AttributeSet, AttributeSet> goodAttributes = await attributeSetTable.Where(attributeItem => (attributeItem.GoodId == good.Id)).ToCollectionAsync();

            // Se une el nombre del atributo con su valor en una lista.
            List<AttrNameValue> nameValueList = new List<AttrNameValue>();
            foreach (SiteAttributeSet siteAttribute in siteAttributes)
            {
                var attribute = from attr in goodAttributes
                                where attr.SiteAttributeId == siteAttribute.Id
                                select attr.Value;
                if (attribute.Count() == 1)
                {
                    nameValueList.Add(new AttrNameValue { Name = siteAttribute.Name, Value = attribute.First() });
                }
                else
                {
                    nameValueList.Add(new AttrNameValue { Name = siteAttribute.Name, Value = "-" });
                }

                DetallesTitle.Text = good.Name;
                ListItems.ItemsSource = nameValueList;
                goodImage.Source = new BitmapImage(new Uri("http://www.ol4rent.com/HomeSite/GetImage?goodId=" + good.Id));
            }
            loadingText.Visibility = Visibility.Collapsed;
            loagindBar.Visibility = Visibility.Collapsed;
        }
    }
}